//
//  DetailView.swift
//  TestActorDownload
//
//  Created by 罗万能 on 2025/6/13.
//
import SwiftData
import AVKit
import SwiftUI
import Foundation

struct DetailView: View {
    @Environment(\.modelContext) var modelContext: ModelContext
    @EnvironmentObject var manager: DownloadManager
    var item: DownloadDataModel
    var body: some View {
        VStack{
            Text("Item at \(item.remoteURL)")
            Text("status:\(item.status)")
            if item.localStoredFilePath != nil{
                Text("已下载 localPath:\(item.localStoredFilePath!)")
                Button("dele local"){
                    item.removeLocalFile(id: item.id)
                }
                VideoPlayer(player: AVPlayer(url: item.localStoredFilePath! ))
                
            }else{
                Text("未下载")
                Button("下载 "){
//                    let task = DownloadTask(model: item)
//                    Task{
//                        try await task.start()
//                    }
                }
                if item.status == .downloading{
                    Button("pause "){
                        
                        Task{
                            await manager.pauseTask(id:  item.id)
                        }
                    }
                }
                if item.status == .paused || item.status == .downloading{
                    Button("resume "){
                        
                        Task{
                            try await manager.resumeTask(id:  item.id)
                        }
                    }
                }
                
            }
            
        }
    }
}
